package com.yxw.yxnet_cd_center.common.utils;

import com.yxw.yxnet_cd_center.common.enums.ErrorCodeEnum;
import lombok.Data;
import net.sf.jsqlparser.expression.StringValue;

import java.io.Serializable;

/**
 * @param <T>
 * @author chen*jun
 */
@Data
public class BaseResult<T> implements Serializable {

    private static final long serialVersionUID = -4580349103066442677L;
    /**
     * 调用结果
     */
    private boolean result;
    /**
     * 返回结果
     */
    private T data;
    /**
     * 内部错误码
     */
    private String resultCode;
    /**
     * 内部信息
     */
    private String resultMsg;
    /**
     * 第三方错误代码
     */
    private String primaryErrorCode;
    /**
     * 第三方错误信息
     */
    private String primaryErrorMsg;
    /**
     * ip地址
     */
    private String primaryErrorIP;

    public BaseResult() {

    }

    public BaseResult(T data) {
        this.result = true;
        this.data = data;
    }

    public BaseResult(String resultCode, String resultMsg) {
        if (null == resultCode) {
            resultCode = "500";
        }
        if (resultCode.trim().equals("")) {
            resultCode = "500";
        }
        this.resultCode = resultCode;
        this.resultMsg = resultMsg;
    }

    public BaseResult(String errorCode, String errorMsg, String primaryErrorCode, String primaryErrorMsg, String primaryErrorIP) {
        if (null == errorCode) {
            errorCode = "500";
        }
        if (errorCode.trim().equals("")) {
            errorCode = "500";
        }
        this.resultCode = errorCode;
        this.resultMsg = errorMsg;
        this.primaryErrorCode = primaryErrorCode;
        this.primaryErrorMsg = primaryErrorMsg;
        this.primaryErrorIP = primaryErrorIP;
    }

    public static BaseResult success() {
        return new BaseResult(true, "200", "");
    }

    public static <T> BaseResult success(T dataTmp) {
        BaseResult oBaseResult = new BaseResult();
        oBaseResult.result = true;
        oBaseResult.resultCode = "200";
        oBaseResult.data = dataTmp;
        return oBaseResult;
    }

    public static <T> BaseResult success(String code, T dataTmp) {
        BaseResult oBaseResult = new BaseResult();
        oBaseResult.result = true;
        oBaseResult.resultCode = code;
        oBaseResult.data = dataTmp;
        return oBaseResult;
    }
    public static <T> BaseResult successWithMsg(String resultMsgTmp) {
        BaseResult oBaseResult = new BaseResult();
        oBaseResult.result = true;
        oBaseResult.resultCode = "200";
        oBaseResult.resultMsg = resultMsgTmp;
        return oBaseResult;
    }
    public static BaseResult failed(String msg) {
        return new BaseResult("200", msg);
    }

    public static BaseResult failed(String code, String msg) {
        return new BaseResult(code, msg);
    }

    public BaseResult(Boolean resultStatus, String errorCode, String errorMessage) {
        super();
        this.result = resultStatus;
        this.resultCode = errorCode;
        this.resultMsg = errorMessage;
    }

    public static <T> BaseResult doSuccess(T date, String msg) {
        BaseResult oBaseResult = new BaseResult();
        oBaseResult.setData(date);
        oBaseResult.setResult(true);
        oBaseResult.setResultCode("200");
        oBaseResult.setResultMsg(msg);
        return oBaseResult;
    }


    /**
     * 错误返回
     * @param msg
     * @return
     */
    public static BaseResult failed(ErrorCodeEnum err, String msg) {
        BaseResult baseResult = new BaseResult();
        baseResult.setResultMsg(msg);
        baseResult.setResult(false);
        baseResult.setResultCode(String.valueOf(err.getCode()));
        return baseResult;
    }

//    /**
//     * 方便链式调用
//     *
//     * @param key 键
//     * @param value 值
//     * @return 数据对象
//     */
//    @Override
//    public BaseResult put(String key, Object value)
//    {
//        super.put(key, value);
//        return this;
//    }
}
